//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import goodsItem from '../comm/goodsItem.vue';
import { scrollMixin } from "utils/mixin";
export default {
  name:'',
  //import引入的组件需要注入到对象中才能使用
  components: {
    goodsItem
  },
  mixins: [scrollMixin], //加载更多
  data() {
  return {
    tabIndex:0,
    status:"",
    page: 1,
    isLoadMore:true,
    orderTab:[
      {
        name:'全部',
        status:'',
        goodsList: [],
        page: 1,
        total_page: 0,
        isLoadMore:true
      },
      {
        name:'待发货',
        status:1,
        goodsList: [],
        page: 1,
        total_page: 0,
        isLoadMore:true
      },
      {
        name:'待签收',
        status:2,
        goodsList: [],
        page: 1,
        total_page: 0,
        isLoadMore:true
      },
      {
        name:'已签收',
        status:3,
        goodsList: [],
        page: 1,
        total_page: 0,
        isLoadMore:true
      }
    ],
    orderApiArr: [
      "plugin.collection-center.frontend.controllers.order.collection-order-list",
      "plugin.collection-center.frontend.controllers.order.collection-order-list.waitSend",
      "plugin.collection-center.frontend.controllers.order.collection-order-list.waitReceive",
      "plugin.collection-center.frontend.controllers.order.collection-order-list.Completed"
    ]
  };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    orderTabOn(status) {
      !status?this.tabIndex = 0:this.tabIndex = status;
      this.status = status;
      window.scrollTo(0, 0);
      if (this.orderTab[this.tabIndex].page >= 2) { //避免切换到其他分类小于当前页码导致重复加载分页
        this.orderTab[this.tabIndex].goodsList = [];
      }
      if (this.orderTab[this.tabIndex].goodsList.length === 0) {
        this.isLoadMore = false;
        this.orderTab[this.tabIndex].page = 1;
        this.orderLists();
      }
    },
    orderstateOn(params) {
      this.getorderState(params);
    },
    getorderState(params) {
      let {value, id, refundId, api,itemBtn,item} = params;
      let query = {
        5:() => {   //确认签收
          this.$dialog
        .confirm({ message: itemBtn.name })
        .then(() => {
          this.confirmOrder_request(id, itemBtn);
        })
        .catch(() => {});
        },
        18:() => {   //确认签收
          this.$router.push(
            this.fun.getUrl("aftersales", {
              refund_id: refundId
            })
          );
        },
        13:() => {   //申请售后
          this.$router.push(
            this.fun.getUrl(
              "refund",
              {
                order_id: id,
                order_type: 1
              },
              { value: value }
            )
          );
        },
        41: `${api}`, //客服
      };
      if (value.toString() === "12") {
        this.$dialog.confirm({
          title: "提示",
          message: "确定删除该订单?"
        })
          .then(() => {
            this.deletePay(item.id, api);
          })
          .catch(() => {
            // on cancel
          }); //删除订单
      } else if (value == 8 || value == "snatch_regiment_express") {
        this.checklogistics(item, itemBtn);
      } else if (value.toString() === "41") {
        if (query[value]) {
          window.location.href = query[value];
        } else { 
          this.$toast('请设置客户链接');
        }
      }
      else {
        let paramsObj = query[value];
        if(paramsObj) {
          return paramsObj.call(this);
        } else {
          console.log("获取未知按钮状态数据失败");
        }
      }
    },
    checklogistics(item, btn) {
      //回放轨迹
      console.log(JSON.parse(window.localStorage.getItem("globalParameter")).delivery_driver_open);
      if (item.dispatch_type_id == 7 && JSON.parse(window.localStorage.getItem("globalParameter")).delivery_driver_open == "1") {
        this.$router.push(
          this.fun.getUrl("track", {
            id: item.id
          })
        );
      }
      if (item.is_all_send_goods == 0) {
        this.$router.push(
          this.fun.getUrl("logistics", {
            id: item.id,
            fromGrab: 1,
            api: btn.api
          },{id: item.id})
        );
      } else {
        //跳转多包裹路由
        // item.is_all_send_goods == 1 || item.is_all_send_goods == 2
        this.$router.push(
          this.fun.getUrl(
            "multiplePackages",
            {
              order_id: item.id //传送订单id到多包裹页面
            },
            { api: btn.api }
          )
        );
      }
    },
    confirmOrder_request(id, btn) {
      //this.$emit('ConfrimOrderNotification');
      //调用接口通知服务器收到货物请求
      $http
        .get(
          btn.api,
          {
            order_id: id
          },
          "操作中..."
        )
        .then(
          (response) => {
            if (response.result == 1) {
              // that.$emit("ConfrimOrderNotification", "pay");
              this.$dialog.alert({ message: response.msg });
              this.orderTab[this.tabIndex].page = 1;
              this.orderLists();
            } else {
              this.$dialog.alert({ message: response.msg });
            }
          },
          function(response) {
            // error callback
            that.noMoreClick = false;
          }
        );
    },
    deletePay(id, api) {
      let json = {
        order_id: id
      };
      $http.post(api, json, "正在获取").then(
        response => {
          if (response.result == 1) {
            this.$toast(response.msg);
            this.orderTab[this.tabIndex].page = 1;
            this.orderLists();
          } else {
            this.$dialog.alert({ message: response.msg });
          }
        },
        function(response) {
          // console.log(response);
          // error callback
        }
      );
    },
    orderLists() {
      let api = this.orderApiArr[this.tabIndex];
      let query = {
        page: this.orderTab[this.tabIndex].page,
        status:this.status
      }
      $http.post(api, query, "加载中..").then(response => {
        if (response.result == 1) {
            let res = response.data;
            let list = res.data;
            this.isLoadMore = true;
            this.orderTab[this.tabIndex].total_page = res.last_page;
            if (!this.orderTab[this.tabIndex].total_page) {
              this.orderTab[this.tabIndex].page = 0;
            }
            this.orderTab[this.tabIndex].goodsList = list;
        } else {
          this.orderTab[this.tabIndex].page = this.orderTab[this.tabIndex].page- 1;
          this.orderTab[this.tabIndex].isLoadMore = false;
          this.$dialog.alert({
            message: response.msg
          });
        }
      });
    },
     //获取更多数据
     getMoreData() {
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.orderTab[this.tabIndex].page >= this.orderTab[this.tabIndex].total_page) {
        return;
      } else {
        this.orderTab[this.tabIndex].page= this.orderTab[this.tabIndex].page + 1;
        let api = this.orderApiArr[this.tabIndex];
        let query = {
          page: this.orderTab[this.tabIndex].page,
          status:this.status
        }
        $http.post(api, query, "加载中").then(
          res => {
            this.isLoadMore = true; 
            if (res.result === 1) {
              let data = res.data;
              let list = data.data;
              // this.isLoadMore= true;
              this.orderTab[this.tabIndex].goodsList = [...this.orderTab[this.tabIndex].goodsList, ...list];
            } else {
              this.orderTab[this.tabIndex].page = this.orderTab[this.tabIndex].page- 1;
              this.isLoadMore = false;
            }
          },
          function(response) {
            console.log("response", response);
            // error callback
          }
        );
      }
    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
  
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
  
  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {
    this.orderTab[this.tabIndex].page = 1;
    this.orderTab.forEach((item,index) => {
      item['goodsList'] = [];
    });
    this.orderLists();
  }, //如果页面有keep-alive缓存功能，这个函数会触发
}